<?php
declare (strict_types = 1);

namespace app\middleware;

use app\BaseController;

class Check extends BaseController
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next,$power){
        $request->cacheInfo = json_decode(app('mycache')->get($_SERVER['HTTP_TOKEN']),true);
        //判断token是否存在
        if (empty($request->cacheInfo)){
            return responseJson(BaseController::NOT_LOGGED_IN,'您还没登录，请跳转到登录页面');
        }
        //判断权限
        if ($power == 'user'){
            if ($request->cacheInfo['jurisdiction'] != 'user'){
                return responseJson(BaseController::NOT_LOGGED_IN,'您还没登录，请跳转到登录页面');
            }
        }
        if ($power == 'root'){
            if ($request->cacheInfo['jurisdiction'] != 'root'){
                return responseJson(BaseController::NOT_LOGGED_IN,'您还没登录，请跳转到登录页面');
            }
        }
    }
}
